/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package res.elements;

import java.awt.Point;
import java.util.ArrayList;

import org.jbox2d.common.Vec2;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;
import plar.core.Sprite;
import plar.core.Player;

public class Health extends Element {

    class CollisionPlayer implements Action {
		int timer;
		Vec2 oldpos;
		String s;
        public CollisionPlayer() {
		timer=0;
        }

        public Parameters call(Parameters parameters)

        {

            Element me = (Element) parameters.get("me");
            String subAction = (String) parameters.get("subAction");
            ArrayList<Element> collisions = (ArrayList<Element>) getProperty("collisionList");

            if (subAction == "main") {
			//	Common.info(8,"timer"+timer);
			if(timer==0)
			{
				//Common.info(8,"timerif"+timer+""+me.body.isSleeping());
				boolean collision=false;
                if (collisions != null)
                {
                    for (Element e : collisions) {
                        //Common.info("ammo collides, exploding warning lol!");
                        if (e.getProperty("type") == "player") {
                            Common.info(7,"player got +1 " + e);
                            Player p = (Player)e;
                            Integer energy = (Integer) e.getProperty("energy");
                        e.setProperty("energy", energy + (Integer)getProperty("energy"));
						collision=true;
                        
         			
            }    
                }}if(collision){
                Common.info(7,"Ammo.CollisionManager.call(): Ammo going to sleep");
                        timer = (Integer)me.getProperty("ammoGap");
                        s=new String(me.getCurrentSpriteSet().get(0).getFileName());
                        Common.info(8,"timerelse"+s);
                        	setStaticSprite("res/none.png");
						}
                }
                else
                {
					//Common.info(8,"timerelse"+timer);
					if(timer==1)
				{	
					//	me.getCurrentSpriteSet().get(0).visible=true;
					me.setStaticSprite(s);
					Common.info(6,"new sprite;"+s);
				}			//getCurrentSpriteSet().clear();
							//getCurrentSpriteSet().add(s);
						//me.setSize(oldsize);
						//me.body.wakeUp();
					
					timer--;
				
				}}
            return null;
        
    
   }
   }
   

    public Health() {
        super();

        setProperty("type", "pack");
        setProperty("isRunnable", true);
        setProperty("ammoGap", 2000);
        setStaticSprite("res/gun.png");
        setProperty("canCollide", true);
		setProperty("isActive", false);
        
        setProperty("energy",10);
        actions.addAction("collision", new CollisionPlayer());
        setProperty("direction", 0); // 0 = right, 1 =left its direction*pigreek
        setSize(0.3f,0.3f);

        

    }

}
